package com.rd.student.ruanjian.class3.gaochenxi.LianXi1.HashMapPractice;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

public class HashMapTest {

      public static void main(String[] args) {
//            HashMap hashMap = new HashMap();
//            hashMap.put("01", 5);
//            hashMap.put("02", 7);
//            hashMap.put("03", 6);
//            hashMap.put("04", 6);
//            hashMap.put("05", 7);
//            hashMap.put("06", 6);
//            HashMapTest.info(hashMap);

            LinkedList list = new LinkedList();

            Student stu1 = new Student(1, "高晨曦", "软件专业", 3, "nice", 20, 876);
            Student stu2 = new Student(2, "张思韬", "软件专业", 3, "nice", 21, 562);
            Student stu3 = new Student(3, "姜万里", "计算机专业", 1, "nice", 22, 832);
            Student stu4 = new Student(4, "郭佳宝", "计算机专业", 1, "nice", 21, 845);
            Student stu5 = new Student(5, "方周", "计算机专业", 1, "nice", 23, 972);
            Student stu6 = new Student(6, "孙昊", "计算机专业", 1, "nice", 22, 1000);
            Student stu7 = new Student(7, "张三", "家里蹲专业", 90, "foolish_man", 90, 999999);

            list.add(stu1);
            list.add(stu2);
            list.add(stu3);
            list.add(stu4);
            list.add(stu5);
            list.add(stu6);
            list.add(stu7);

            HashMap hash1 = HashMapTest.countNum(list, 1);
            info(hash1);
            System.out.println();
            HashMap hash2 = HashMapTest.countNum(list, 2);
            info(hash2);
            System.out.println();
            HashMap hash3 = HashMapTest.countNum(list, 3);
            info(hash3);

      }
      public static HashMap countNum(LinkedList list,int type){
            HashMap hashMap=new HashMap();//这是一个新的hashMap集合
            for(int i=0;i<list.size();i++){
                  Student stu=(Student) list.get(i);
                  String str="";
                  switch (type){
                        case 1:
                              str= String.valueOf(stu.cla);//以班级为Key ,数量为value
                              break;
                        case 2:
                              str=stu.major;//以专业为Key ,数量为value
                              break;
                        case 3:
                              str= String.valueOf(stu.age);//以年龄为Key ,数量为value
                              break;
                        default:
                              System.out.println("error");
                              return hashMap;
                  }
                  if(hashMap.containsKey(str)==false){//如果hashMap中的对象的对应的
                        hashMap.put(str,1);//这个str这个属性共出现过1次
                  }else{
                        /*
                              get(Object key)
                             返回指定键所映射的值；如果对于该键来说，此映射不包含任何映射关系，则返回 null。
                         */
                        int value= (int) hashMap.get(str);//获得了这个key的value值，也就是它出现的次数
                        hashMap.put(str,value+1);//等到它下一次出现，value(它出现的次数)+1
                  }
            }
            return hashMap;
      }


      //ArrayList和LinkedList都有自己的遍历方式，这个是遍历hashMap的方式,得到hashMap的键，得到hashMap的值
      public static  void info(HashMap hashMap){
            Set set=hashMap.keySet();
            Iterator it=set.iterator();
            while(it.hasNext()){
                  Object key = it.next();
                  Object value=hashMap.get(key);
                  System.out.print("<"+key+", "+value+">"+"    ");
            }
      }

      // 小组成员
      // 统计班级为key,每个班级的人数为value
      // 统计专业为key,每个专业的人数为value
      // 统计年龄为key,每个年龄的人数为value
}
class Student {
      //学号既序号
      private int num;
      //姓名
      public String name;
      //专业
      public String major;
      //班级
      public int cla;
      //评论
      public String desc;
      //年龄
      public int age;

      private int codeName;

      public Student() {

      }

      public Student(String name, String major, int cla) {
            this.name = name;
            this.major = major;
            this.cla = cla;
      }

      public Student(int num, String name, String major, int cla, String desc, int age, int codeName) {
            this.num = num;
            this.name = name;
            this.major = major;
            this.cla = cla;
            this.desc = desc;
            this.age = age;
            this.codeName = codeName;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getMajor() {
            return major;
      }

      public void setMajor(String major) {
            this.major = major;
      }

      public String getDesc() {
            return desc;
      }

      public void setDesc(String desc) {
            this.desc = desc;
      }

      public int getAge() {
            return age;
      }

      public void setAge(int age) {
            this.age = age;
      }

      public int getCla() {
            return cla;
      }

      public void setCla(int cla) {
            this.cla = cla;
      }

      public int getCodeName() {
            return codeName;
      }

      public void setCodeName(int codeName) {
            this.codeName = codeName;
      }

      public int getNum() {
            return num;
      }

      public void setNum(int num) {
            this.num = num;
      }


      @Override
      public String toString() {
            return "[" + "name='" + name + '\'' + ']';
      }
}
